﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.ComponentModel;

namespace VsSpeakerCompanion.UI
{
    /// <summary>
    /// Interaction logic for LoginControl.xaml
    /// </summary>
    internal partial class EndingSessionControl : UserControl
    {
        internal static readonly DependencyProperty RemainingTimeToCancelEndSessionProperty =
            DependencyProperty.Register(
                "RemainingTimeToCancelEndSession",
                typeof(int),
                typeof(EndingSessionControl));

        private const int TimeToCancelEndSession = 5;

        internal int RemainingTimeToCancelEndSession
        {
            get { return (int)GetValue(RemainingTimeToCancelEndSessionProperty); }
            private set { SetValue(RemainingTimeToCancelEndSessionProperty, value); }
        }

        internal event EventHandler Comitted;
        internal  event EventHandler Cancelled;
        private Timer timer;

        internal EndingSessionControl()
        {
            InitializeComponent();

            this.DataContext = this;

            timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
        }

        void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                RemainingTimeToCancelEndSession--;

                if (RemainingTimeToCancelEndSession == 0)
                {
                    Stop();

                    OnComitted(new EventArgs());
                }
            }));
        }

        /// <summary>
        /// Raises the Comitted event
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnComitted(EventArgs e)
        {
            if (Comitted != null)
            {
                Comitted(this, e);
            }
        }

        /// <summary>
        /// Raises the Cancelled event
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnCancelled(EventArgs e)
        {
            if (Cancelled != null)
            {
                Cancelled(this, e);
            }
        }

        internal void Start()
        {
            this.RemainingTimeToCancelEndSession = TimeToCancelEndSession;
            this.timer.Start();
        }

        internal void Stop()
        {
            this.timer.Stop();
        }

        private void OnCancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.Stop();
            OnCancelled(new EventArgs());
        }
    }
}